package com.hssy.wululuaplan.config;

import com.hssy.wululuaplan.interceptor.LoginInterceptorV2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * web相关配置
 * @author Mr.Hongtao
 */
@Configuration
public class WebmvcConfig implements WebMvcConfigurer {
    @Resource
    private LoginInterceptorV2 loginInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                // 允许跨域的域名，可以用*表示允许任何域名使用。实际项目中请设置为指定的域名，否则任何地址都能登录
                .allowedOrigins("*")
                // *允许任何请求头
                .allowedHeaders("*")
                // 允许携带cookie信息
                .allowCredentials(true)
                //.exposedHeaders("access-control-allow-headers",
                //"access-control-allow-methods",
                //"access-control-allow-origin",
                //"access-control-max-age",
                //"X-Frame-Options")
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                // 对所有的路径都进行拦截
                .addPathPatterns("/**")
                // 唯独放行登录接口和swagger资源
                .excludePathPatterns("/api/login", "/api/register")
                .excludePathPatterns("/swagger-resources/**","/v2/**");
    }

}
